package com.kaishengit.util;


import java.util.ArrayList;
import java.util.List;

public class Pager<T> {

	private int totalNums;
	private int pageSize;
	private int pageNum;
	private List<T> result;
	
	public Pager(int pageSize,int totalNums,int pageNum) {
		setPageSize(pageSize);
		setTotalNums(totalNums);
		setPageNum(pageNum);
	}
	
	public Pager(int totalNums,int pageNum) {
		this(10,totalNums,pageNum);
	}
	
	
	public int getTotalNums() {
		return totalNums;
	}
	
	public void setTotalNums(int totalNums) {
		this.totalNums = totalNums;
	}
	
	
	public int getPageSize() {
		return pageSize;
	}
	
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	
	
	public int getTotalPages() {
		
		int total = this.totalNums / this.pageSize;
		
		if(this.totalNums % this.pageSize != 0) {
			total += 1;
		}
		
		return total;
	}

	
	public int getStartNum() {
		int num = (this.pageNum - 1) * this.pageSize;
		return num;
	}
	
	
	public int getPageNum() {
		return pageNum;
	}
	public void setPageNum(int pageNum) {
		if(pageNum < 1) {
			pageNum = 1;
		} else if(pageNum > getTotalPages()) {
			pageNum = getTotalPages();
		}
		this.pageNum = pageNum;
	}

	public List<T> getResult() {
		return result;
	}

	public void setResult(List<T> result) {
		this.result = result;
	}

	public List<Integer> getSlider() {
		int count = 5;
		int halfSize = count / 2;
		int totalPage = getTotalPages();

		int startPageNo = Math.max(this.pageNum - halfSize, 1);
		int endPageNo = Math.min(startPageNo + count - 1, totalPage);

		if (endPageNo - startPageNo < count) {
			startPageNo = Math.max(endPageNo - count, 1);
		}

		List<Integer> result = new ArrayList<Integer>();
		for (int i = startPageNo; i <= endPageNo; i++) {
			result.add(i);
		}
		return result;
	}
}

